#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>

#pragma pack(1)
typedef struct
{
	char unused[18];
	long width;
	long height;
	short planes;
	short bpp;
	long compression;
	char unused2[20];
}TBitmapHeader;
#pragma pack()

GLuint loadBitmap(char *path)
{
	FILE *f = fopen(path,"rb");
	if (!f)
		return -1;

	TBitmapHeader header;

	fread(&header, sizeof(header),1,f);

	char *data;
	int sz = header.width*header.height*3;

	data = (char*)malloc(sz);

	fread(data,sz,1,f);
	fclose(f);

	for (int i=0;i<sz;i+=3)
	{
		int tmp = data[i];
		data[i] = data[i+2];
		data[i+2]=tmp;
	}

	GLuint texture[1];

	glGenTextures(1, &texture[0]);					// Create The Texture

	// Typical Texture Generation Using Data From The Bitmap
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, header.width, header.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	return texture[0];
}